package com.alennus.blog.service;

import com.alennus.blog.domain.AccountRole;
import com.alennus.blog.domain.Account;
import com.alennus.blog.domain.Menu;
import com.alennus.blog.domain.Role;
import com.alennus.blog.repository.AccountRepository;
import com.alennus.blog.utils.DateUtils;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by pactera on 2018/5/14.
 */
@Service
public class AccountService {


    @Value("${media.server.url}")
    private String mediaServerUrl;
    @Autowired
    private AccountRepository accountRepository;
    @Autowired
    private MongoTemplate template;

    @Autowired
    private AccountRoleService accountRoleService;
    @Autowired
    private RoleService roleService;

    @Transactional
    public Account saveOrUpdate(Account account){
        List<AccountRole> addAccountRoles = Lists.newArrayList();
        List<String> roleIds = account.getRoleIds();
        String accountId = account.getId();
        accountRoleService.delete(accountId);
        for(String id: roleIds){
            AccountRole accountRole = new AccountRole();
            accountRole.setAccountId(account.getId());
            accountRole.setRoleId(id);
            accountRole.setCreatedDate(DateUtils.getCurrentDatetime());
            accountRole.setUpdateDate(DateUtils.getCurrentDatetime());
            addAccountRoles.add(accountRole);
        }
        accountRoleService.saveOrUpdate(addAccountRoles);
        return accountRepository.save(account);
    }

    public Account findByID(String id){
        return accountRepository.findById(id).get();
    }
    public void delete(List<Account> accounts){
        accountRepository.deleteAll(accounts);
    }

    public Page<Account> findAccount(Account account, Pageable pageable){
        Criteria criteria = new Criteria();
//        if(!StringUtils.isEmpty(menu.getParentId())){
//            criteria.and("parentId").is(menu.getParentId());
//        }
        Query query = new Query(criteria);
        //查询
        List<Account> list = template.find(query.with(pageable), Account.class);
        for(Account account1 : list){
            List<String> roleIds = Lists.newArrayList();
            String icon = account1.getIcon();
            List<AccountRole> accountRoles = accountRoleService.findByAccountId(account1.getId());
            for(AccountRole accountRole : accountRoles){
                roleIds.add(accountRole.getRoleId());
            }
            account1.setRoleIds(roleIds);
            if(StringUtils.isEmpty(icon)){
                icon = "default.jpg";
            }
            account1.setBackground(mediaServerUrl+"/"+icon);
        }

        Long count =  template.count(query, Account.class);
        //将集合与分页结果封装
        return new PageImpl<>(list, pageable, count);
    }


}
